package drds.configuration.db.xml_mapping.sql_execute;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MapperInterfaceProxyFactory<T> {
    private final Class<T> mapperInterface;
    private final Map<Method, MapperMethod> methodToMapperMethodMap = new ConcurrentHashMap<>();

    public MapperInterfaceProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public T newInstance(MapperExecutionContext mapperExecutionContext) {
        final MapperProxy<T> mapperProxy = new MapperProxy<>(mapperInterface, methodToMapperMethodMap, mapperExecutionContext);
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
    }

}
